package ru.mirent;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import org.junit.jupiter.api.Test;

import java.util.List;

public class JacksonExampleTest {

    @Test
    void enableTypingTest() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()
                .allowIfSubType("java.util.List")
                .build();
        objectMapper.activateDefaultTyping(ptv);
        ExampleGetter exampleGetter = new ExampleGetter(List.of("Элемент 1", "Элемент 2"));
        System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(exampleGetter));
    }

    @Test
    void disableTypingTest() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.deactivateDefaultTyping();
        ExampleGetter exampleGetter = new ExampleGetter(List.of("Элемент 1", "Элемент 2"));
        System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(exampleGetter));
    }

    static class ExampleGetter {
        @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
        private final List<String> getterValue;

        public ExampleGetter(List<String> getterValue) {
            this.getterValue = getterValue;
        }

    }
}
